//
//  ViewController.m
//  VS
//
//  Created by qi tan on 2023/12/18.
//

#import "ViewController.h"
#import "VSSystemTableViewController.h"
#import "VSGalleryViewController.h"
#import "AVFoundation/AVFoundation.h"


//#define mPath @"rtsp://192.168.8.1:554/livestream/1"
#define mPath @"https://o.devio.org/files/video/v=NGoOpniJwnM.mp4"


@interface ViewController ()
// 拖线控件存放处，按住Ctrl拖线
@property (weak, nonatomic) IBOutlet UISegmentedControl *segement;
- (IBAction)segementChanged:(id)sender; // 点击事件

// tab0，tab1，tab2
@property (weak, nonatomic) IBOutlet UIView *uiViewTab00; // 预览
@property (weak, nonatomic) IBOutlet UIView *uiViewTab01; // 分划
@property (weak, nonatomic) IBOutlet UIView *uiViewTab02; // 主页（相册，系统设置）
- (IBAction)goToGallery:(id)sender;

// 视频播放
@property (weak, nonatomic) IBOutlet UIView *videoView;

@property (strong, nonatomic)AVPlayer *myPlayer;//播放器
@property (strong, nonatomic)AVPlayerItem *item;//播放单元
@property (strong, nonatomic)AVPlayerLayer *playerLayer;//播放界面（layer）

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    #pragma mark - 设置导航栏内容
    // 返回按钮
    NSString *backStr = NSLocalizedString(@"back", nil);
    self.navigationItem.backButtonTitle= backStr;
    
    [self initVideoPlay];
}

- (void)initVideoPlay {
    NSURL *mediaURL = [NSURL URLWithString: mPath]; // 播放的网址
    
    self.item = [AVPlayerItem playerItemWithURL:mediaURL]; // 播放单元
    self.myPlayer = [AVPlayer playerWithPlayerItem:self.item]; // 播放对象

    // 播放器的layer
    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.myPlayer];
    // self.playerLayer.frame = CGRectMake(0, 0, self.view.bounds.size.width, 240);
    int height = self.view.bounds.size.width * 251 / 393;
    self.playerLayer.frame = CGRectMake(0, 0, self.view.bounds.size.width, height);
    [self.videoView.layer addSublayer:self.playerLayer];
    // 开始播放
    [self.myPlayer play];
}




- (IBAction)goToGallery:(id)sender {
    NSLog(@"jump to goToGallery");
    VSGalleryViewController *gVC = [[VSGalleryViewController alloc] init];
    [self.navigationController pushViewController:gVC animated:YES];
}



#pragma mark - 底部tab切换

- (IBAction)segementChanged:(id)sender {
    int Index = (int)self.segement.selectedSegmentIndex;
    NSLog(@"select: %d", Index);
    switch (Index) {
        case 0: // home
            self.uiViewTab00.hidden=NO;
            self.uiViewTab01.hidden=YES;
            self.uiViewTab02.hidden=YES;
            break;
        case 1: // 预览
            self.uiViewTab00.hidden=YES;
            self.uiViewTab01.hidden=NO;
            self.uiViewTab02.hidden=YES;
            break;
        case 2: // 分化
            self.uiViewTab00.hidden=YES;
            self.uiViewTab01.hidden=YES;
            self.uiViewTab02.hidden=NO;
            break;
        default:
            break;
    }
}




@end
